<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;
use \Exception;

class ZlActivePrizrUserInfo extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_active_prizr_user_info';

    protected $cacheModel = 'prize.user_prize_info';

    /**
     * 获取用户抽奖信息
     * @param int $uid
     * @param int $activeId
     * @param array $field
     * @return array
     * @throws Exception
     */
    public function getUserPrizeInfo(int $uid, int $activeId, array $field = []): array
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid . ':' . $activeId)->getDataAndSet(function () use ($uid, $activeId)
        {
            try
            {
                $data = $this->where(['active_id' => $activeId, 'uid' => $uid])->field('id,uid,active_id,prize_unsuc_num,prize_lucky_num,senior_peize_num,total_prize_num,potential,create_time')->find();
                if (!$data)
                {
                    $initData = [
                        'uid' => $uid,
                        'active_id' => $activeId,
                        'prize_unsuc_num' => 0,  //  未抽奖次数
                        'prize_lucky_num' => 0,  //  已抽奖次数
                        'senior_peize_num' => 0,  //  高级抽奖次数
                        'total_prize_num' => 0,  //  总共可抽奖次数
                        'potential' => 0,   // 能力值
                        'create_time' => time(),
                    ];

                    $this->save($initData);

                    $initData['id'] = $this->id;

                    $data = $initData;
                } else
                {
                    $data = $data->toArray();
                }
            }
            catch (Exception $e)
            {
                $data = [];
                Log::write("获取用户抽奖信息失败；用户ID【{$uid}】活动ID【{$activeId}】;错误信息: . {$e->getMessage()}；错误文件「{$e->getFile()}」错误行数「{$e->getLine()}」", 'error', true);
            }
            return $data;
        }, $field);
    }

    /**
     * 更新用户抽奖信息
     * @param int $uid 用户ID
     * @param int $activeId 活动ID
     * @param array $updateData 用户抽奖更新信息
     * @param int $id 用户抽奖信息ID
     * @return bool
     */
    public function updateData(int $uid, int $activeId, array $updateData, int $id = 0): bool
    {
        try
        {
            if ($id)
            {
                $this->save($updateData, ['id' => $id]);
            } else
            {
                $this->save($updateData, ['active_id' => $activeId, 'uid' => $uid]);
            }
            // 更新缓存
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid . ':' . $activeId)->setCache($updateData);
        }
        catch (Exception $e)
        {
            Log::write("更新用户抽奖信息失败；用户ID【{$uid}】活动ID【{$activeId}】更新数据【" . \GuzzleHttp\json_encode($updateData, JSON_UNESCAPED_UNICODE) . "】;错误信息: . {$e->getMessage()}；错误文件「{$e->getFile()}」错误行数「{$e->getLine()}」", 'error', true);
            return false;
        }

        return true;
    }
}
